#include "client.h"
#include <termios.h>   
#include <unistd.h>     
#include <errno.h>  
#include <ctype.h>
#define USER_PASSWD_LENGTH 20

char Getch()    
{  
    int c = 0;    
    int res = 0;      
    struct termios org_opts, new_opts;   
    res = tcgetattr(STDIN_FILENO, &org_opts);   //用于获取与终端相关的参数  
    if(res == -1)
    {
        printf("tcgetattr error! Error code: %d，Error message: %s\n", errno, strerror(errno)); 
        c = '\n';
        return c;

    }

    memcpy(&new_opts, &org_opts, sizeof(new_opts));  
    new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL); 

    res = tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);   //用于设置终端参数
    if(res == -1)
    {
        printf("tcsetattr error! Error code: %d，Error message: %s\n", errno, strerror(errno)); 
        c = '\n';
        return c;

    }
    c = getchar();      
    res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts); 
    if(res == -1)
    {
        printf("tcsetattr error! Error code: %d，Error message: %s\n", errno, strerror(errno)); 
        c = '\n';
        return c;

    }

    return c;  

}  

char *getString(int lengthLimit)
{
      char *strRet = (char *)malloc(lengthLimit);
    memset(strRet, 0, lengthLimit);

    unsigned char c;
    int i = 0;

    while ((c = Getch()) != '\n')
    {
        
        if (i<lengthLimit && isprint(c))
        {
            strRet[i++] = c;
            putchar(c);
        }
        else if ((i>0) && (c == 127))
        {
            --i;
            putchar('\b');
            putchar(' ');
            putchar('\b');
        }

    }
    strRet[i] = '\0';

    return strRet;

}
char *getPasswd(int lengthLimit)
{
    char *strRet = (char *)malloc(USER_PASSWD_LENGTH);
    memset(strRet, 0, USER_PASSWD_LENGTH);

    unsigned char c;
    int i = 0;

    while ((c = Getch()) != '\n')
    {
        if (i<lengthLimit && isprint(c))
        {
            strRet[i++] = c;
            putchar('*');
        }
        else if ((i>0) && (c == 127))
        {
            --i;
            putchar('\b');
            putchar(' ');
            putchar('\b');
        }
    }
    strRet[i] = '\0';

    return strRet;
}


int userLogin(int socketFd,userStat_t *puser)
{
    char *passwd;
    char *userName;
    printf("input your account name: ");
    fflush(stdout);
    userName = getString(64);
    printf("\n");

    printf("input your password: ");
    fflush(stdout);
    passwd = getPasswd(20);
    printf("\n");

    int ret = 0;
    train_t sender;
    sender.dataLen = strlen(userName);
    strcpy(sender.buf, userName);
    ret = send(socketFd, &sender, sizeof(int) + sender.dataLen, 0);
    if(-1 == ret)
    {
        perror("send1 in userlogin");
        free(userName);
        free(passwd);
        return -1;
    }
    sender.dataLen = strlen(passwd);
    strcpy(sender.buf, passwd);
    ret = send(socketFd, &sender, sizeof(int) + sender.dataLen, 0);
    if(-1 == ret)
    {
        perror("send2 in userlogin");
        free(userName);
        free(passwd);
        return -1;
    }
    
    //接收服务器反馈信息；
    int flag = 0;
    recvCyc(socketFd, &flag, sizeof(int));
    if(flag)
    {
        printf("success!\n");
        free(userName);
        free(passwd);
        return 0;
    }
    else
    {
        printf("failed!\n");
        free(userName);
        free(passwd);
        return -1;
    }
    
}